#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>

using namespace std;

class Solution {
public:
    // 1218. 最长定差子序列
    // https://leetcode.cn/problems/longest-arithmetic-subsequence-of-given-difference/
    int longestSubsequence(vector<int>& arr, int difference) {
        int n = arr.size();
        unordered_map<int, int> hash;
        hash[arr[0]] = 1;
        int ans = 1;
        for (int i = 1; i<n; ++i)
        {
            hash[arr[i]] = hash[arr[i] - difference] + 1;
            ans = max(ans, hash[arr[i]]);
        }

        return ans;
    }
};